package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.model.vo.req.BaseVo;
import com.moli.iov.admin.model.vo.req.stat.*;
import com.moli.iov.admin.service.IStatFacadeService;
import com.moli.iov.admin.utils.ListUtil;
import com.moli.iov.alarm.model.dto.StatAlarmDTO;
import com.moli.iov.alarm.remote.IAlarmStatRemoteService;
import com.moli.iov.his.model.dto.StatOnlineDTO;
import com.moli.iov.his.model.dto.StatOnlineReportDTO;
import com.moli.iov.his.model.dto.StatOnlineReqDTO;
import com.moli.iov.his.remote.IHisStatRemoteService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.enums.UserTypeEnum;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StatFacadeServiceImpl implements IStatFacadeService {

    @Reference(group = "${service.group}", check = false)
    IHisStatRemoteService hisStatRemoteService;

    @Reference(group = "${service.group}", check = false)
    IAlarmStatRemoteService alarmStatRemoteService;

    @Override
    public RestResponse<StatOnlineVo> getStatOnline(StatOnlineReqDTO reqDto) {

        StatOnlineVo vo =  new StatOnlineVo();
        RestResponse<StatOnlineReportDTO> res = hisStatRemoteService.getStatOnline(reqDto);
        if (RestResponse.isSuccess(res)) {
            List<StatOnlineLineVo> list = new ArrayList<>();
            for (StatOnlineDTO dto : res.getData().getList()) {
                if (dto != null) {
                    StatOnlineLineVo lineVo = new StatOnlineLineVo();
                    CommonUtils.copyProperties(dto, lineVo);
                    list.add(lineVo);
                }
            }
            vo.setList(list);
            vo.setFieldMap(res.getData().getFieldMap());
        }

        return RestResponse.success(vo);
    }

    @Override
    public RestResponse getStatFenceAlarm(BaseVo baseVo, Integer statType, Integer alarmStatType) {

        Long companyId = null;
        if (UserTypeEnum.COMPANY.getFlag() == baseVo.getSysUserType()
                || UserTypeEnum.PLATFORM.getFlag() == baseVo.getSysUserType()) {
            companyId = baseVo.getSysCompanyId();
        } else {
            return RestResponse.success();
        }

        RestResponse<StatAlarmDTO> rest = alarmStatRemoteService.getStatAlarm(companyId, statType);
        System.out.println(JsonUtils.toJson(rest));
        if (RestResponse.isSuccess(rest)) {

            Map<String, String> map = new HashMap<>();

            Class clazz = null;
            Class ratioClazz = StatAlarmRatioVo.class;
            StatAlarmVo statAlarmVo = new StatAlarmVo();
            switch (alarmStatType) {
                case 1:
                    clazz = StatFenceAlarmLineVo.class;
                    map.put("outFenceAlarmCount", "出围栏报警");
                    map.put("inFenceAlarmCount", "进围栏报警");
                    map.put("title", "围栏报警统计");
                    statAlarmVo.setFieldMap(map);
                    statAlarmVo.setWeek(ListUtil.copyProperties(rest.getData().getWeekFenceRatioList(), ratioClazz));
                    statAlarmVo.setMonth(ListUtil.copyProperties(rest.getData().getMonthFenceRatioList(), ratioClazz));
                    statAlarmVo.setPieChat(ListUtil.copyProperties(rest.getData().getFenceRatioList(), ratioClazz));
                    statAlarmVo.setList(ListUtil.copyProperties(rest.getData().getList(), clazz));

                    return RestResponse.success(statAlarmVo);
                case 2:
                    clazz = StatLevelAlarmLineVo.class;

                    map.put("oneLvAlarmCount", "一级");
                    map.put("twoLvAlarmCount", "二级");
                    map.put("threeLvAlarmCount", "三级");
                    map.put("title", "车载终端报警级别统计");
                    statAlarmVo.setFieldMap(map);
                    //复制列表内元素
                    statAlarmVo.setWeek(ListUtil.copyProperties(rest.getData().getWeekLevelRatioList(), ratioClazz));
                    statAlarmVo.setMonth(ListUtil.copyProperties(rest.getData().getMonthLevelRatioList(), ratioClazz));
                    statAlarmVo.setPieChat(ListUtil.copyProperties(rest.getData().getLevelRatioList(), ratioClazz));
                    statAlarmVo.setList(ListUtil.copyProperties(rest.getData().getList(), clazz));

                    return RestResponse.success(statAlarmVo);
                case 3:
                    clazz = StatPreventionAlarmLineVo.class;
                    map.put("offLineCount", "失联车辆");
                    map.put("mileageExceptionChangeCount", "里程异变");
                    map.put("title", "车辆失联报警统计");
                    //复制列表内元素
                    statAlarmVo.setList(ListUtil.copyProperties(rest.getData().getList(), clazz));
                    statAlarmVo.setFieldMap(map);
                    statAlarmVo.setWeek(ListUtil.copyProperties(rest.getData().getWeekRiskRatioList(), ratioClazz));
                    statAlarmVo.setMonth(ListUtil.copyProperties(rest.getData().getMonthRiskRatioList(), ratioClazz));
                    statAlarmVo.setPieChat(ListUtil.copyProperties(rest.getData().getRiskRatioList(), ratioClazz));

                    return RestResponse.success(statAlarmVo);
                default:
                    log.info("无该报警统计类型");
                    return RestResponse.success();
            }

        }
        log.info("无查询结果");
        return RestResponse.success();
    }
}
